package com.ry.novel.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.model.dto.*;
import com.ry.novel.model.dto.condition.BookQuery;
import com.ry.novel.model.dto.condition.CommentQuery;
import com.ry.novel.model.entity.Book;
import com.ry.novel.model.resp.Result;
import com.ry.novel.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 不需要登录就能访问的接口
 * @author ry
 * @since 2022-02-19 11:33
 **/
@RestController
public class ReaderController {
    @Autowired
    private BookService bookService;

    @Autowired
    private BookCommentService bookCommentService;

    @Autowired
    private BookIndexService bookIndexService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private NewsService newsService;

    @GetMapping("/index")
    public Result<Map<String,List<BookInfo>>> index(){
        Map<String, List<BookInfo>> map = bookService.readerIndex();

        return Result.ok(map);
    }

    @GetMapping("/bookDetail")
    public Result<BookInfo> bookDetail(@RequestParam("id")Long id){
        BookInfo bookInfo = bookService.queryById(id);


        return Result.ok(bookInfo);
    }

    @GetMapping("/readFirstIndex")
    public Result<BookIndexInfo> readFirstIndex(@RequestParam("bookId")Long bookId){
        return Result.ok(bookIndexService.readFirstIndex(bookId));
    }

    @GetMapping("/queryIndexById")
    public Result<BookIndexInfo> queryIndexById(@RequestParam("indexId")Long indexId){
        BookIndexInfo bookIndexInfo = bookIndexService.readerQueryIndexById(indexId);

        return Result.ok(bookIndexInfo);
    }

    @GetMapping("/queryIndicesByBookId")
    public Result<List<BookIndexInfo>> queryIndicesByBookId(@RequestParam("bookId")Long bookId){
        List<BookIndexInfo> bookIndices = bookIndexService.queryIndicesByBookId(bookId);

        return Result.ok(bookIndices);
    }

    @GetMapping("/queryAllCategory")
    public Result<List<CategoryInfo>> queryAllCategory(){
        return Result.ok(categoryService.getAllCategory());
    }

    @PostMapping("/getBookByCategory")
    public Result<Page<BookInfo>> getBookByCategory(@RequestBody BookQuery bookQuery){
        Page<BookInfo> infoPage =  bookService.queryBookByCategory(bookQuery);

        return Result.ok(infoPage);
    }

    @PostMapping("/getBookByRank")
    public Result<Page<BookInfo>> getBookByRank(@RequestBody BookQuery bookQuery){
        Page<BookInfo> infoPage = bookService.queryBookByRank(bookQuery);

        return Result.ok(infoPage);
    }

    /**
     * 根据书籍id，分页查询评论
     */
    @PostMapping("/queryCommentByBookId")
    public Result<Page<BookCommentInfo>> queryCommentByBookId(@RequestBody CommentQuery commentQuery){
        Page<BookCommentInfo> infoPage = bookCommentService.queryByBookId(commentQuery);

        return Result.ok(infoPage);
    }

    @PostMapping("/searchBookByPage")
    public Result<Page<BookInfo>> searchBookByPage(@RequestBody BookQuery bookQuery){
        Page<BookInfo> infoPage = bookService.searchByPage(bookQuery);

        return Result.ok(infoPage);
    }

    /**
     * 返回true表示是电脑,返回false表示不是电脑
     * @param request
     * @return
     */
    @GetMapping("/isComputer")
    public Result<Boolean> isComputer(HttpServletRequest request){
        Boolean flag = true;

        String userAgent = request.getHeader("user-agent");
        System.out.println(userAgent);

        if (userAgent.contains("Android") || userAgent.contains("iPhone")){
            flag = false;
        }

        return Result.ok(flag);
    }
}
